<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: address_book_process.php 438 2014-01-19 11:50:30Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 438 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2014-01-19 11:50:30 +0000 (Sun, 19 Jan 2014) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */
  define('GLOBAL_COLUMN_LEFT', '0');
include('includes/application_top.php');
$smarty = new Smarty();
require(DIR_FS_CATALOG.'templates/'.CURRENT_TEMPLATE.'/source/boxes.php');
require_once(DIR_FS_INC.'inc.count_customer_address_book_entries.php');
require_once(DIR_FS_INC.'inc.address_label.php');

if (!isset($_SESSION['customer_id']))
	redirect(href_link(FILENAME_LOGIN, '', 'SSL'));

if (isset($_GET['action']) && ($_GET['action'] == 'deleteconfirm') && isset($_GET['delete']) && is_numeric($_GET['delete'])) {
	$db->db_query("DELETE FROM ".TABLE_ADDRESS_BOOK." WHERE address_book_id = '".(int) $_GET['delete']."' AND customers_id = '".(int) $_SESSION['customer_id']."'");

	$message_stack->add_session(SUCCESS_ADDRESS_BOOK_ENTRY_DELETED, 'success');
	redirect(href_link(FILENAME_ADDRESS_BOOK, '', 'SSL'));
}

if (isset($_GET['delete']) && is_numeric($_GET['delete'])) {
	if ($_GET['delete'] == $_SESSION['customer_default_address_id']) {
		$message_stack->add_session(WARNING_PRIMARY_ADDRESS_DELETION, 'warning');

		redirect(href_link(FILENAME_ADDRESS_BOOK, '', 'SSL'));
	} else {
		$check = $db->db_query("SELECT 
									COUNT(*) AS total 
								FROM 
									".TABLE_ADDRESS_BOOK." 
								WHERE 
									address_book_id = '".(int) $_GET['delete']."' 
								AND 
									customers_id = '".(int) $_SESSION['customer_id']."'");

		if ($check->fields['total'] < 1) {
			$message_stack->add_session(ERROR_NONEXISTING_ADDRESS_BOOK_ENTRY, 'error');
			redirect(href_link(FILENAME_ADDRESS_BOOK, '', 'SSL'));
		}
	}
}

require(DIR_WS_CLASSES.'class.input_fields.php');
$field = new input_fields('create_account', $_POST);

$entry = '';
if(isset($_GET['edit']) && !empty($_GET['edit'])) {
	$entry = $db->db_query("SELECT 
								entry_gender, 
								entry_company, 
								entry_firstname, 
								entry_lastname, 
								entry_street_address, 
								entry_suburb, 
								entry_postcode, 
								entry_city, 
								entry_state, 
								entry_zone_id, 
								entry_country_id 
							FROM 
								".TABLE_ADDRESS_BOOK." 
							WHERE 
								customers_id = '".(int)$_SESSION['customer_id']."' 
							AND
								address_book_id = '".(int)$_GET['edit']."'");
								
	if (!$entry->_numOfRows) {
		$message_stack->add_session(ERROR_NONEXISTING_ADDRESS_BOOK_ENTRY, 'error');
		redirect(href_link(FILENAME_ADDRESS_BOOK, '', 'SSL'));
	}
}

$gender_male = $field->getField('gender', 'm', (!isset($_POST) ? ($entry->fields['entry_gender'] == 'm' ? ' checked=""' : '') : ''));
$gender_female = $field->getField('gender', 'f', (!isset($_POST) ? ($entry->fields['entry_gender'] == 'f' ? ' checked=""' : '') : ''));
$firstname = $field->getField('firstname', $entry->fields['entry_firstname']);
$lastname = $field->getField('lastname', $entry->fields['entry_lastname']);
$street_address = $field->getField('street_address', $entry->fields['entry_street_address'], 'size="21" class="create_account_street"');
$postcode = $field->getField('postcode', $entry->fields['entry_postcode'], 'size="5"  class="create_account_postcode"');
$city = $field->getField('city', $entry->fields['entry_city'], 'size="19" class="create_account_city"');
$state = $field->getField('state', $entry->fields['entry_state'], 'class="create_account_state"');
$suburb = $field->getField('suburb', $entry->fields['entry_suburb']);
$country = $field->getField('country', $entry->fields['entry_country_id'], 'class="create_account_state" id="country"');
$company = $field->getField('company', $entry->fields['entry_company']);

$process = false;
if ((isset($_POST['action']) && (($_POST['action'] == 'process') || ($_POST['action'] == 'update'))) && $field->error != 'true') {
	$process = true;

	$sql_data_array = array('entry_firstname' => $field->post['firstname'], 
							'entry_lastname' => $field->post['lastname'], 
							'entry_street_address' => $field->post['street_address'], 
							'entry_postcode' => $field->post['postcode'], 
							'entry_city' => $field->post['city'], 
							'entry_country_id' => (int)$field->post['country'],
							'entry_state' => $field->post['state'],
							'entry_suburb' => $field->post['suburb'],
							'address_last_modified' => 'NOW()');
									
	if ($_POST['action'] == 'update') {
		$db->db_perform(TABLE_ADDRESS_BOOK, $sql_data_array, 'update', "address_book_id = '".(int)$_GET['edit']."' AND customers_id ='".(int)$_SESSION['customer_id']."'");

		if ((isset($_POST['primary']) && ($_POST['primary'] == 'on')) || ($_GET['edit'] == $_SESSION['customer_default_address_id'])) {
			
			if(!empty($field->post['gender']))
				$sql_data_array['customers_gender'] = $field->post['gender'];
			
			$_SESSION['customer_first_name'] = $field->post['firstname'];
			$_SESSION['customer_country_id'] = (int)$field->post['country'];
			$_SESSION['customer_zone_id'] = '0';
			$_SESSION['customer_default_address_id'] = (int)$_GET['edit'];

			$sql_data_array = array('customers_firstname' => $field->post['firstname'], 
									'customers_lastname' => $field->post['lastname'], 
									'customers_default_address_id' => (int)$_GET['edit'],
									'customers_last_modified' => 'NOW()');

			$db->db_perform(TABLE_CUSTOMERS, $sql_data_array, 'update', "customers_id = '".(int)$_SESSION['customer_id']."'");
		}
		
	} else {
		$sql_data_array['customers_id'] = (int)$_SESSION['customer_id'];
		$sql_data_array['address_date_added'] = 'NOW()';
		
		if(!empty($field->post['gender']))
			$sql_data_array['entry_gender'] = $field->post['gender'];

		$db->db_perform(TABLE_ADDRESS_BOOK, $sql_data_array);

		$new_address_book_id = $db->db_insert_id();

		if (isset($_POST['primary']) && ($_POST['primary'] == 'on')) {
			$_SESSION['customer_first_name'] = $field->post['firstname'];
			$_SESSION['customer_country_id'] = (int)$field->post['country'];
			$_SESSION['customer_zone_id'] = '0';
			
			if (isset($_POST['primary']) && ($_POST['primary'] == 'on'))
				$_SESSION['customer_default_address_id'] = $new_address_book_id;

			$sql_data_array = array('customers_firstname' => $field->post['firstname'], 
									'customers_lastname' => $field->post['lastname'],
									'customers_last_modified' => 'NOW()',
									'customers_date_added' => 'NOW()');
				
			if (isset($_POST['primary']) && ($_POST['primary'] == 'on'))
				$sql_data_array['customers_default_address_id'] = $new_address_book_id;

			$db->db_perform(TABLE_CUSTOMERS, $sql_data_array, 'update', "customers_id = '".(int)$_SESSION['customer_id']."'");
		}
	}
	$message_stack->add_session(SUCCESS_ADDRESS_BOOK_ENTRY_UPDATED, 'success');
	redirect(href_link(FILENAME_ADDRESS_BOOK, '', 'SSL'));
}

if (!isset($_GET['delete']) && !isset($_GET['edit'])) {
	if(count_customer_address_book_entries() >= MAX_ADDRESS_BOOK_ENTRIES) {
		$message_stack->add_session(ERROR_ADDRESS_BOOK_FULL, 'error');
		redirect(href_link(FILENAME_ADDRESS_BOOK, '', 'SSL'));
	}
}

$breadcrumb->add(NAVBAR_TITLE_1_ADDRESS_BOOK_PROCESS, href_link(FILENAME_ACCOUNT, '', 'SSL'));
$breadcrumb->add(NAVBAR_TITLE_2_ADDRESS_BOOK_PROCESS, href_link(FILENAME_ADDRESS_BOOK, '', 'SSL'));

if (isset($_GET['edit']) && is_numeric($_GET['edit']))
	$breadcrumb->add(NAVBAR_TITLE_MODIFY_ENTRY_ADDRESS_BOOK_PROCESS, href_link(FILENAME_ADDRESS_BOOK_PROCESS, 'edit='.$_GET['edit'], 'SSL'));
	
elseif (isset($_GET['delete']) && is_numeric($_GET['delete']))
	$breadcrumb->add(NAVBAR_TITLE_DELETE_ENTRY_ADDRESS_BOOK_PROCESS, href_link(FILENAME_ADDRESS_BOOK_PROCESS, 'delete='.$_GET['delete'], 'SSL'));
	
else
	$breadcrumb->add(NAVBAR_TITLE_ADD_ENTRY_ADDRESS_BOOK_PROCESS, href_link(FILENAME_ADDRESS_BOOK_PROCESS, '', 'SSL'));

require (DIR_WS_INCLUDES.'header.php');

if (isset($_GET['delete']) == false)
	$action = draw_form('addressbook', href_link(FILENAME_ADDRESS_BOOK_PROCESS, (isset($_GET['edit']) ? 'edit='.$_GET['edit'] : ''), 'SSL'));

$smarty->assign('FORM_ACTION', $action);

if (isset($_GET['delete'])) {
	$smarty->assign('delete', '1');
	$smarty->assign('ADDRESS', address_label($_SESSION['customer_id'], $_GET['delete'], true, ' ', '<br />'));

	$smarty->assign('BUTTON_BACK', '<a href="'.href_link(FILENAME_ADDRESS_BOOK, '', 'SSL').'">'.image_button('button_back.gif', IMAGE_BUTTON_BACK).'</a>');
	$smarty->assign('BUTTON_DELETE', '<a href="'.href_link(FILENAME_ADDRESS_BOOK_PROCESS, 'delete='.$_GET['delete'].'&action=deleteconfirm', 'SSL').'">'.image_button('button_delete.gif', IMAGE_BUTTON_DELETE).'</a>');
	
} else {

	$module_smarty = new Smarty();
	$module_smarty->assign('INPUT_MALE', $gender_male);
	$module_smarty->assign('INPUT_FEMALE', $gender_female);
	$module_smarty->assign('INPUT_FIRSTNAME', $firstname);
	$module_smarty->assign('INPUT_LASTNAME', $lastname);
	$module_smarty->assign('INPUT_STREET', $street_address);
	$module_smarty->assign('INPUT_CODE', $postcode);
	$module_smarty->assign('INPUT_CITY', $city);
	$module_smarty->assign('INPUT_COUNTRY', $country);
	$module_smarty->assign('INPUT_SUBURB', $suburb);
	$module_smarty->assign('INPUT_STATE', $state);
	$module_smarty->assign('INPUT_COMPANY', $company);
	
	if((isset($_GET['edit']) && ($_SESSION['customer_default_address_id'] != $_GET['edit'])) || (isset($_GET['edit']) == false)) {
		$module_smarty->assign('new', '1');
		$module_smarty->assign('CHECKBOX_PRIMARY', draw_checkbox_field('primary', 'on', false, 'id="primary"'));
	}
	
	if(isset($_GET['edit']))
		$module_smarty->assign('ADDRESS_BOOK_TITLE', ADDRESS_BOOK_EDIT_ENTRY);
	else
		$module_smarty->assign('ADDRESS_BOOK_TITLE', ADDRESS_BOOK_NEW_ENTRY);
	
	$module_smarty->assign('language', $_SESSION['language']);
	$module_smarty->caching = false;
	$main_content = $module_smarty->fetch(CURRENT_TEMPLATE.'/module/address_book_details.html');
	$smarty->assign('MODULE_address_book_details', $main_content);
	
	if (isset($_GET['edit']) && is_numeric($_GET['edit'])) {
		$smarty->assign('BUTTON_BACK', '<a href="'.href_link(FILENAME_ADDRESS_BOOK, '', 'SSL').'">'.image_button('button_back.gif', IMAGE_BUTTON_BACK).'</a>');
		$smarty->assign('BUTTON_UPDATE', draw_hidden_field('action', 'update').draw_hidden_field('edit', $_GET['edit']).image_submit('button_update.gif', IMAGE_BUTTON_UPDATE));
	} else {
		$smarty->assign('BUTTON_BACK', '<a href="'.href_link(FILENAME_ADDRESS_BOOK, '', 'SSL').'">'.image_button('button_back.gif', IMAGE_BUTTON_BACK).'</a>');
		$smarty->assign('BUTTON_UPDATE', draw_hidden_field('action', 'process').image_submit('button_continue.gif', IMAGE_BUTTON_CONTINUE));
	}
	$smarty->assign('FORM_END', '</form>');
}

$smarty->assign('language', $_SESSION['language']);
$smarty->caching = false;
$main_content = $smarty->fetch(CURRENT_TEMPLATE.'/module/address_book_process.html');

$smarty->assign('language', $_SESSION['language']);
$smarty->assign('main_content', $main_content);
$smarty->caching = false;
if (!defined('RM'))
	$smarty->loadFilter('output', 'note');
$smarty->loadFilter('output','trimwhitespace');
$smarty->display(CURRENT_TEMPLATE.'/index.html');
include ('includes/application_bottom.php');
?>